package com.example.fee.service;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.fee.mapper.SysFeeMapper;
import com.example.fee.po.SysFee;
import com.exmaple.cdc.listener.CDCMessageListener;
import com.exmaple.cdc.service.SysMQService;
import com.exmaple.cdc.wrap.BinlogAddObj;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Map;


@Service
public class SysFeeServiceImpl extends ServiceImpl<SysFeeMapper, SysFee> implements SysFeeService {

    Logger logger = LoggerFactory.getLogger(getClass());

    @Resource
    private SysMQService sysMQService;

    /****
     * 接受消息并处理消息
     * @param binlogAddObj
     */
    @Override
    public void addUserFee(BinlogAddObj binlogAddObj) {
        for (Map<String, Serializable> map : binlogAddObj.getRowsList()) {
            if (map.containsKey(CDCMessageListener.ID) && map.get(CDCMessageListener.ID) != null) {
                long nextId = IdUtil.getSnowflakeNextId();
                String modelId = (String) map.get(CDCMessageListener.MODEL_ID);
                SysFee sysFee = new SysFee(nextId, modelId, 100);
                sysFee.recalculateFields();
                this.saveOrUpdate(sysFee);
                this.sysMQService.publish(sysFee.getClass().getName(), nextId, "sys_user_register_fee_message", modelId);
            }
        }
    }
}
